// pages/onlineOrdering/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    player:0,
    carts:[],
    proIndex:null,
    attrIndex:0,
    totalNum:0,
    totalPrice:0,
    result:[],
    empty:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideShareMenu()
    let that = this
    
    var pages = getCurrentPages(); 
    var prevPage = pages[pages.length - 2];   //上一页
    console.log('prevPage',prevPage.data.productList)
    this.setData({
      productList:prevPage.data.productList
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  input:function(e){
    var key=e.currentTarget.dataset.name
    this.setData({
      [key]:e.detail.value
    })
  },
  submit:function(e){
    var vals=e.detail.value
    var productList=this.data.productList
    if(!vals.keyword){
      wx.showToast({
        title: '请输入关键词',
        icon:'none',
        duration:1500
      })
      return false
    }
    var result=productList.filter(item=>{return item.name.includes(vals.keyword)})
    console.log(result)
    if(result.length==0){
      this.setData({
        empty:true
      })
    }else{
      this.setData({
        empty:false
      })
    }
    this.setData({
      keyword:vals.keyword,
      result:result
    })
  

  },
  //关闭弹窗
  close:function(){
    this.setData({
      player:0,
      attrIndex:0,
    })
  },
  //打开规格弹窗
  chooseAttr:function(e){
    var userInfo=wx.getStorageSync('userInfo')
    if(!userInfo.mobile){
      wx.navigateTo({
        url: '/pages/login/index',
      })
      return false
    }
    var index=e.currentTarget.dataset.index
    this.setData({
      proIndex:index,
      player:1
    })
  },
  //选择规格
  selectArr:function(e){
    var index=e.currentTarget.dataset.index
    this.setData({
      attrIndex:index
    })
  },
  addtoCart:function(){
   
    var proIndex=this.data.proIndex
    var attrIndex=this.data.attrIndex
    
    var key='productList['+proIndex+'].specifications['+attrIndex+'].num'
    console.log(key)
    this.setData({
        [key]:1
    })
    this.updateCart()
  },
  //减少
  reduce:function(e){
    var productlist=this.data.productList
    console.log(e)
    var aindex=e.currentTarget.dataset.aindex
    if(aindex>=0){
      this.setData({
        attrIndex:aindex
      })
    }
    var proIndex=e.currentTarget.dataset.index
    var attrIndex=this.data.attrIndex

    var num=productlist[proIndex].specifications[attrIndex].num
    num--
    if(num<0){
      num=0
    }
    var key='productList['+proIndex+'].specifications['+attrIndex+'].num'
    console.log(key)
    this.setData({
        [key]:num
    })
    this.updateCart()
  },
  //增加
  add:function(e){
    var userInfo=wx.getStorageSync('userInfo')
    if(!userInfo.mobile){
      wx.navigateTo({
        url: '/pages/login/index',
      })
      return false
    }
    var productlist=this.data.productList
    var aindex=e.currentTarget.dataset.aindex
    if(aindex>=0){
      this.setData({
        attrIndex:aindex
      })
    }
    var proIndex=e.currentTarget.dataset.index
    var attrIndex=this.data.attrIndex
    var num=productlist[proIndex].specifications[attrIndex].num
    var snum=productlist[proIndex].num
    var stockNum=productlist[proIndex].stockNum
    console.log(snum,stockNum)
    if(snum<stockNum){
      num++
    }else{
      return false
    }
    
    var key='productList['+proIndex+'].specifications['+attrIndex+'].num'
    console.log(key)
    this.setData({
        [key]:num
    })
    this.updateCart()
  },

  //更新购物车
  updateCart:function(){
    var that=this
    var productList=this.data.productList
    var jsonArr=[]
    productList.map((item,index)=>{
      var nums=0
      item.specifications.map((ite,i)=>{
        if(ite.num>0){
          var jsonStr={
            name:item.name,
            productId:item.id,
            image:item.smallImage,
            price:Number(item.price*ite.num).toFixed(2),
            specifications:ite.name,
            num:ite.num,
            proIndex:index,
            attrIndex:i
          }
          nums += ite.num
          jsonArr.push(jsonStr)
        }
      })
      item.num=nums
    })
    var totalPrice=0
    var totalNum=0
    jsonArr.map(item=>{
      totalPrice += Number(item.price)
      totalNum += parseInt(item.num)
    })
    console.log(totalPrice,jsonArr,totalNum)
    this.setData({
      totalPrice:totalPrice,
      carts:jsonArr,
      totalNum:totalNum,
      productList:productList
    })

    var pages = getCurrentPages(); 
    var prevPage = pages[pages.length - 2];   //上一页
    prevPage.setData({
      totalPrice:totalPrice,
      carts:jsonArr,
      totalNum:totalNum,
      productList:productList
    })
    if(jsonArr.length==0){
      if(that.data.player==2){
        that.setData({
          player:0,
          attrIndex:0
        })
      }
    }

  },
})